package com.ecube.test.control;

import com.alibaba.fastjson.JSON;
import com.ecube.mqtt.MqttUtils;
import com.ecube.protocol.ProtocolHelper;
import com.ecube.protocol.common.DataReportHead;
import com.ecube.protocol.common.DataReportStandard;
import com.ecube.protocol.enums.WindFunctionEnum;
import com.ecube.protocol.wind.WindPowerAdjustSetResp;
import com.ecube.utils.EnumUtils;
import lombok.Builder;
import lombok.Data;

/**
 * 风能
 *  功率调节设置-回复
 *
 * @author zhuweijun
 * @version 1.0.0
 * @since 2025/8/7 14:46
 */
public class SendWindControl {

    public static void main(String[] args) {
        sendPowerAdjustSetResp("FAT", "100090022", "20250731", WindInfo.builder().effect(2).result(1).index(51).build());
        sendPowerAdjustSetResp("FAT", "100090022", "20250806", WindInfo.builder().effect(2).result(2).reason(0).index(50).build());
    }

    /**
     * 功率调节设置-回复
     *
     * @param env env
     * @param productCode productCode
     * @param deviceCode deviceCode
     * @param info info
     */
    private static void sendPowerAdjustSetResp(String env, String productCode, String deviceCode, WindInfo info){
        WindFunctionEnum function = (WindFunctionEnum) EnumUtils.getFunction(WindPowerAdjustSetResp.class);
        if(function == null){
            return;
        }

        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();

        WindPowerAdjustSetResp adjustSetResp = new WindPowerAdjustSetResp();

        DataReportStandard reportStandard = new DataReportStandard();
        DataReportHead reportHand = ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction());
        reportHand.setVersion("V1.0.1");
        reportHand.setIndex(info.getIndex());
        reportStandard.setHeader(reportHand);

        // 设置储能信息
        adjustSetResp.setEffect(info.getEffect());
        adjustSetResp.setResult(info.getResult());
        adjustSetResp.setReason(info.getReason());
        reportStandard.setDataBody(adjustSetResp);

        String reportContent = JSON.toJSONString(reportStandard);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);
    }

    @Data
    @Builder
    private static class WindInfo {
        /**
         * 结果：1-成功；2-失败
         */
        private Integer result = 1;

        /**
         * 生效类型：1-立即生效；2-取消生效；
         */
        private Integer effect;

        /**
         * 失败原因: 0-功能不可用；1-参数错误；
         */
        private Integer reason;

        /**
         * 序号
         */
        private Integer index;
    }
}
